** This do-file extracts parliamentary election results from DPI

use "$project_path/data/1_input/elections/DPI/DPI2017.dta", clear
keep countryname year gov*me gov*seat gov*vote opp*me opp*seat opp*vote legelec totalseats

rename countryname Country
rename year Year

// Adjust country names
replace Country="Ivory Coast"  if Country=="Cote d'Ivoire"
replace Country="Korea, Republic of"  if Country=="ROK"
replace Country="Serbia" if Country=="Yugoslavia"
replace Country="Yemen" if Country=="Yemen (AR)"
drop if Country=="Yemen (PDR)"
drop if Country==""

// Renaming results variables
forvalues i = 1/3 {
	ren gov`i'me Party_`i' 
	ren gov`i'seat Seats_`i' 
	ren gov`i'vote Vote_Share1_`i'
	local j = `i'+3
	ren opp`i'me Party_`j' 
	ren opp`i'seat Seats_`j' 
	ren opp`i'vote Vote_Share1_`j'
}

// Missing values 
forvalues i=1/6 {
	replace Party_`i' = "" if Party_`i'=="-999"
	replace Seats_`i' = . if Seats_`i'==-999
	replace Vote_Share1_`i' = . if Vote_Share1_`i'==-999
	gen temp`i' = ((Party_`i'=="NA" | Party_`i'=="na") & (Seats_`i'==0 | Seats_`i'==.) & (Vote_Share1_`i'==0 | Vote_Share1_`i'==.))
	replace Party_`i' = "" if temp`i'==1
	replace Seats_`i' = . if temp`i'==1
	replace Vote_Share1_`i' = . if temp`i'==1
	drop temp`i'
}

// Manually adjusting election events
replace legelec = . if legelec==-999
replace legelec = 1 if Country=="Algeria" & Year==1991
replace legelec = 0 if Country=="Algeria" & Year==1992
replace legelec = 0 if Country=="Algeria" & Year==1993
replace legelec = 1 if Country=="Colombia" & Year==1998
replace legelec = 0 if Country=="Colombia" & Year==1999
replace legelec = 0 if Country=="Congo" & Year==2006
replace legelec = 0 if Country=="Czech Rep." & Year==2012
replace legelec = 0 if Country=="Egypt" & Year==2012
replace legelec = 1 if Country=="Eq. Guinea" & Year==1983
replace legelec = 0 if Country=="Eq. Guinea" & Year==1984
replace legelec = 0 if Country=="Eritrea" & Year==1994
replace legelec = 1 if Country=="Estonia" & Year==1995
replace legelec = 0 if Country=="Estonia" & Year==1996
replace legelec = 0 if Country=="Estonia" & Year==1997
replace legelec = 0 if Country=="Haiti" & Year==2017
replace legelec = 1 if Country=="Iraq" & Year==1980
replace legelec = 0 if Country=="Iraq" & Year==1979
replace legelec = 0 if Country=="Japan" & Year==2010
replace legelec = 1 if Country=="Kenya" & Year==1988
replace legelec = 0 if Country=="Kenya" & Year==1987
replace legelec = 0 if Country=="Lebanon" & Year==2010
replace legelec = 1 if Country=="Maldives" & Year==1994
replace legelec = 0 if Country=="Maldives" & Year==1995
replace legelec = 1 if Country=="Mali" & Year==1985
replace legelec = 0 if Country=="Mali" & Year==1984
replace legelec = 0 if Country=="Netherlands" & Year==1991
replace legelec = 1 if Country=="Slovakia" & Year==2016
replace legelec = 0 if Country=="Slovakia" & Year==2015
replace legelec = 0 if Country=="South Sudan" & Year==2011
replace legelec = 0 if Country=="Sri Lanka" & Year==1988
replace legelec = 0 if Country=="Suriname" & Year==1976
replace legelec = 0 if Country=="Taiwan" & Year==1996
replace legelec = 0 if Country=="Taiwan" & Year==2005
replace legelec = 1 if Country=="Thailand" & Year==1986
replace legelec = 1 if Country=="Thailand" & Year==1988
replace legelec = 0 if Country=="Thailand" & Year==1987
replace legelec = 1 if Country=="UAE" & Year==2015
replace legelec = 0 if Country=="UAE" & Year==2016
replace legelec = 1 if Country=="Uganda" & Year==1994
replace legelec = 0 if Country=="Uganda" & Year==1992
replace legelec = 1 if Country=="Argentina" & Year==1985
replace legelec = 1 if Country=="Argentina" & Year==2015
replace legelec = 1 if Country=="Belgium" & Year==1977
replace legelec = 1 if Country=="Brazil" & Year==1978
replace legelec = 1 if Country=="Brazil" & Year==1994
replace legelec = 1 if Country=="Bulgaria" & Year==1981
replace legelec = 1 if Country=="Bulgaria" & Year==1990
replace legelec = 1 if Country=="Bulgaria" & Year==1994
replace legelec = 1 if Country=="Bulgaria" & Year==2013
replace legelec = 1 if Country=="C. Verde Is." & Year==1980
replace legelec = 1 if Country=="Cameroon" & Year==1978
replace legelec = 1 if Country=="Cyprus" & Year==1976
replace legelec = 1 if Country=="Djibouti" & Year==1982
replace legelec = 1 if Country=="Djibouti" & Year==1987
replace legelec = 1 if Country=="Djibouti" & Year==1992
replace legelec = 1 if Country=="El Salvador" & Year==2000
replace legelec = 1 if Country=="GDR" & Year==1990
replace legelec = 1 if Country=="Greece" & Year==1990
replace legelec = 1 if Country=="Guatemala" & Year==1978
replace legelec = 1 if Country=="Haiti" & Year==2000
replace legelec = 1 if Country=="India" & Year==1998
replace legelec = 1 if Country=="Japan" & Year==2012
replace legelec = 1 if Country=="Kenya" & Year==1979
replace legelec = 1 if Country=="Kyrgyzstan" & Year==2010
replace legelec = 1 if Country=="Laos" & Year==1989
replace legelec = 1 if Country=="Mauritius" & Year==1983
replace legelec = 1 if Country=="Myanmar" & Year==1981
replace legelec = 1 if Country=="Oman" & Year==2003
replace legelec = 1 if Country=="Oman" & Year==2015
replace legelec = 1 if Country=="Panama" & Year==1991
replace legelec = 1 if Country=="Peru" & Year==1992
replace legelec = 1 if Country=="Korea, Republic of" & Year==1978
replace legelec = 1 if Country=="Russia" & Year==1995
replace legelec = 1 if Country=="Sri Lanka" & Year==2000
replace legelec = 1 if Country=="Taiwan" & Year==1983
replace legelec = 1 if Country=="Taiwan" & Year==1986
replace legelec = 1 if Country=="Taiwan" & Year==1989
replace legelec = 1 if Country=="Taiwan" & Year==1992
replace legelec = 1 if Country=="Taiwan" & Year==1998
replace legelec = 1 if Country=="Thailand" & Year==1976
replace legelec = 1 if Country=="Turkmenistan" & Year==2008
replace legelec = 1 if Country=="Serbia" & Year==1992
replace legelec = 1 if Country=="Turk Cyprus" & Year==1981
replace legelec = 1 if Country=="Turk Cyprus" & Year==1985
replace legelec = 1 if Country=="Turk Cyprus" & Year==1990
replace legelec = 1 if Country=="Azerbaijan" & Year==2000
replace legelec = 1 if Country=="Bulgaria" & Year==1997
replace legelec = 1 if Country=="Finland" & Year==1975

// Reordering parties
reshape long Party_ Seats_ Vote_Share1_, i(Country Year) j(oldid)
drop oldid
gsort Country Year -Seats_ -Vote_Share1_ Party_
gen double Seat_Share_ = Seats_/totalseats
drop Vote_Share1_
by Country Year: gen id = _n
reshape wide Party_ Seats_ Seat_Share_, i(Country Year) j(id)

// Variables corresponding to results at t+1 and t+2
foreach v of varlist Party_* Seats_* Seat_Share_* {
	gen p_`v' = `v'[_n+1] if Country==Country[_n+1]
	gen p2_`v' = `v'[_n+2] if Country==Country[_n+2]
}

// We get results from the year after the election, which is the most common case
foreach v of varlist Party_* Seats_* Seat_Share_* {
	gen _`v' = p_`v' if legelec==1
}

// Keeping only election years
keep if legelec==1

// Cases in which the seat shares are updated on the year of the election
gen change_t = .
replace change_t = 1 if legelec==1 & Country=="Belgium" & Year==1999
replace change_t = 1 if legelec==1 & Country=="Croatia" & Year==2000
replace change_t = 1 if legelec==1 & Country=="Denmark" & Year==1998
replace change_t = 1 if legelec==1 & Country=="Dom. Rep." & Year==1998
replace change_t = 1 if legelec==1 & Country=="Ecuador" & Year==1998
replace change_t = 1 if legelec==1 & Country=="Fiji" & Year==1994
replace change_t = 1 if legelec==1 & Country=="Finland" & Year==1991
replace change_t = 1 if legelec==1 & Country=="Finland" & Year==2003
replace change_t = 1 if legelec==1 & Country=="GDR" & Year==1986
replace change_t = 1 if legelec==1 & Country=="Gambia" & Year==1997
replace change_t = 1 if legelec==1 & Country=="Gambia" & Year==2002
replace change_t = 1 if legelec==1 & Country=="Grenada" & Year==1999
replace change_t = 1 if legelec==1 & Country=="Iran" & Year==1988
replace change_t = 1 if legelec==1 & Country=="Iran" & Year==2000
replace change_t = 1 if legelec==1 & Country=="Kuwait" & Year==2012
replace change_t = 1 if legelec==1 & Country=="Latvia" & Year==1998
replace change_t = 1 if legelec==1 & Country=="Malta" & Year==2003
replace change_t = 1 if legelec==1 & Country=="Norway" & Year==1985
replace change_t = 1 if legelec==1 & Country=="Philippines" & Year==1984
replace change_t = 1 if legelec==1 & Country=="Romania" & Year==1975
replace change_t = 1 if legelec==1 & Country=="Romania" & Year==1980
replace change_t = 1 if legelec==1 & Country=="Rwanda" & Year==1988
replace change_t = 1 if legelec==1 & Country=="Senegal" & Year==1988
replace change_t = 1 if legelec==1 & Country=="Senegal" & Year==1993
replace change_t = 1 if legelec==1 & Country=="Singapore" & Year==1988
replace change_t = 1 if legelec==1 & Country=="Singapore" & Year==1991
replace change_t = 1 if legelec==1 & Country=="St. Lucia" & Year==1992
replace change_t = 1 if legelec==1 & Country=="Tanzania" & Year==1975
replace change_t = 1 if legelec==1 & Country=="Thailand" & Year==2001
replace change_t = 1 if legelec==1 & Country=="Trinidad-Tobago" & Year==2002
replace change_t = 1 if legelec==1 & Country=="Tunisia" & Year==1981
replace change_t = 1 if legelec==1 & Country=="Myanmar" & Year==1985
replace change_t = 1 if legelec==1 & Country=="Sierra Leone" & Year==1986
replace change_t = 1 if legelec==1 & Country=="Sudan" & Year==1978
foreach v of varlist Party_* Seats_* Seat_Share_* {
	replace _`v' = `v' if change_t==1
}
drop change_t

// Cases in which the seat shares are updated only on year t+2
gen change_tp2 = .
replace change_tp2 = 1 if legelec==1 & Country=="Australia" & Year==1984
replace change_tp2 = 1 if legelec==1 & Country=="Guatemala" & Year==1990
replace change_tp2 = 1 if legelec==1 & Country=="Haiti" & Year==2015
replace change_tp2 = 1 if legelec==1 & Country=="P. N. Guinea" & Year==1987
replace change_tp2 = 1 if legelec==1 & Country=="Somalia" & Year==1979
replace change_tp2 = 1 if legelec==1 & Country=="Timor-Leste" & Year==2001
replace change_tp2 = 1 if legelec==1 & Country=="Congo" & Year==1989
replace change_tp2 = 1 if legelec==1 & Country=="Guinea" & Year==1981
replace change_tp2 = 1 if legelec==1 & Country=="Malawi" & Year==1983
replace change_tp2 = 1 if legelec==1 & Country=="Myanmar" & Year==1978
replace change_tp2 = 1 if legelec==1 & Country=="Zambia" & Year==1983
foreach v of varlist Party_* Seats_* Seat_Share_* {
	replace _`v' = p2_`v' if change_tp2==1
}
drop change_tp2

// In some cases, we shouldn't use DPI as a source (because seat shares are not updated after an election or results are otherwise inaccurate)
drop if Country=="Denmark" & Year==1987
drop if Country=="Denmark" & Year==1988
drop if Country=="Egypt" & Year==2010
drop if Country=="Georgia" & Year==2003
drop if Country=="Greece" & Year==1989
drop if Country=="Mexico" & Year==1991
drop if Country=="S. Africa" & Year==1984
drop if Country=="Samoa" & Year==1988
drop if Country=="Syria" & Year==1994
drop if Country=="Trinidad-Tobago" & Year==2001
drop if Country=="UK" & Year==2015
drop if Country=="Ukraine" & Year==2012
drop if Country=="South Yemen" & Year==1986
drop if Country=="Congo" & Year==1992
drop if Country=="Mauritius" & Year==1982
drop if Country=="Mauritius" & Year==1983
drop if Country=="Mauritius" & Year==1987
drop if Country=="Thailand" & Year==1976
drop if Country=="Uzbekistan" & Year==1994
drop if Country=="Ivory Coast" & Year==2000

// Keeping only key variables
keep Country Year _* totalseats
forvalues k = 1/6 {
	ren _Party_`k' Party_`k'
	ren _Seats_`k' Seats_`k'
	ren _Seat_Share_`k' Seat_Share_`k'
	order Party_`k' Seats_`k' Seat_Share_`k', last
}
format Party_* %25s

// Drop when results are missing
drop if Seats_1==.

// Total number of seats
ren totalseats Total_Seats
replace Total_Seats = . if Total_Seats==0
gen Type_Election = "Parliamentary"
gen Month = .
order Country Year Month Type_Election Total_Seats

// Adding missing variables
forvalues k = 7/75 {
	gen Party_`k' = ""
	gen Seats_`k' = .
	gen Seat_Share_`k' = .
}
recast double Seat_Share_*
foreach v in flag_wrong_sum_seats flag_inconsequential flag_vacant_seats flag_vacant_seats_nb flag_appointed flag_appointed_nb flag_non_partisan flag_coup {
	gen `v' = .
}
gen flag_inconsequential_note = ""
order flag_inconsequential_note, after(flag_inconsequential)

// Updating month and year variable when adequate
replace Month = 3 if Country=="Colombia" & Year==1990
replace Month = 10 if Country=="Congo" & Year==1993
replace Month = 6 if Country=="Greece" & Year==1989
replace Month = 6 if Country=="Greece" & Year==2012
replace Month = 1 if Country=="Greece" & Year==2015
replace Month = 2 if Country=="Ireland" & Year==1982
replace Month = 12 if Country=="Kuwait" & Year==2012
replace Month = 6 if Country=="Libya" & Year==2014
replace Month = 11 if Country=="Turkey" & Year==2015
replace Month = 7 if Country=="Moldova" & Year==2009
replace Month = 9 if Country=="Thailand" & Year==1992
replace Month = 4 if Country=="Iceland" & Year==1991
replace Month = 3 if Country=="Fiji" & Year==1977
replace Month = 6 if Country=="Bangladesh" & Year==1996
replace Month = 12 if Country=="Serbia" & Year==1992
replace Month = 12 if Country=="Iraq" & Year==2005
replace Month = 8 if Country=="Mali" & Year==1997
replace Year=2012 if Country=="Egypt" & Year==2011
replace Year=2005 if Country=="Uzbekistan" & Year==2004
replace Year=2010 if Country=="Uzbekistan" & Year==2009

// Detecting elections which have the wrong sum of seats
egen sum_seats_parties = rowtotal(Seats_*)
replace flag_wrong_sum_seats = 1 if sum_seats_parties!=Total_Seats
replace flag_wrong_sum_seats = 1 if Total_Seats==0
replace flag_wrong_sum_seats = 1 if Total_Seats==.
drop sum_seats_parties

lab var Country  "Country in which election took place"
lab var Year     "Year of the election"
lab var Month    "Month of the election"
lab var Type_Election    "Election type"
lab var flag_wrong_sum_seats		"Sum of elected seats in parliament is inconsistent with the total number of seats"
lab var flag_inconsequential		"Flags cancelled or inconsequential elections"
lab var flag_inconsequential_note		"Why flag_inconsequential is equal to 1"
lab var flag_coup		"Flags elections that were shortly followed by a coup or revolution"
lab var flag_vacant_seats		"Flags elections which left vacant seats in parliament"
lab var flag_vacant_seats_nb		"Number of vacant seats in parliament"
lab var flag_appointed		"Flags elections in which some seats were not elected directly"
lab var flag_appointed_nb		"Number of seats for which MPs were not elected directly"
lab var flag_non_partisan		"Flags elections with no parties"
lab var Total_Seats			"Total number of seats"
forvalues i=1/75 {
	lab var Party_`i'        "Party or coalition n°`i'"
	lab var Seats_`i'  "Seats won by party n°`i'"
	lab var Seat_Share_`i'  "Seat share of party n°`i'"
	replace Seat_Share_`i' = 100*Seat_Share_`i'
}

compress
format Country %30s
format Type_Election %15s
format Party_* %25s
recast double Seat_Share_*
notes drop _dta

save "$project_path/data/2_intermediary/elections/Parliamentary elections/parl_elec_dpi", replace
